package com.talkyun.openx;

import com.talkyun.utils.http.HttpConnector;
import com.talkyun.utils.http.HttpResult;

import java.util.HashMap;
import java.util.Map;

import static java.util.concurrent.TimeUnit.SECONDS;

public class RestfulGeneralClient {

    public RestfulGeneralClient() {
    }

    public RestfulGeneralClient(String url) {

    }

    //    public void delete(String repoName, String filePath) {
    //        String path = filePath.startsWith("/") ? filePath : "/" + filePath;
    //        CodeItem item = getCodeItem(repoName, filePath);
    //        if (item == null) {
    //            throw new RuntimeException("File not exists! " + filePath);
    //        }
    //
    //        // DELETE https://gitee.com/api/v5/repos/{owner}/{repo}/contents/{path}
    //        String uri = getBaseUri(repoName, path);
    //        Map<String, Object> body = new HashMap<String, Object>();
    //        body.put("access_token", accessToken);
    //        body.put("owner", repoOwner);
    //        body.put("repo", repoName);
    //        body.put("path", path);
    //        body.put("sha", item.getSha());
    //        body.put("message", "ccp del " + getTimeStamp());
    //        body.put("branch", "master");
    //
    //        this.doHttpRequest("DELETE", uri, body);
    //    }

    protected String doHttpRequest(String method, String uri, Map<String, Object> body) {
        String apiUrl = null;
        String full = apiUrl + uri;
        HttpConnector http = new HttpConnector(full, (int) SECONDS.toMillis(5), true);
        Map<String, String> header = new HashMap<String, String>();
        header.put("Content-Type", "application/json;charset=UTF-8");

        if (body != null && !body.containsKey("access_token")) {
            Object accessToken = null;
            body.put("access_token", accessToken);
        }

        // Gson gson = new GsonBuilder().create();

        try {
            HttpResult result = null;// http.sendWithHeader(method, header, body == null ? null : gson.toJson(body));
            if (isNormalStatus(result.getStatus())) {
                return result.getResult();
            }
            throw new RuntimeException(result.getResult());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private boolean isNormalStatus(int status) {
        return status == 200 || status == 201 || status == 204;
    }

    public Object call(String urlOrUri, String method, Object... args) {
        // build http call
        // get http result
        //  - to se object ( primitive or map or list<map> )

        return new Object();
    }


    // Object result = rest.call("helloService", new String[] { "java.lang.String" }, new Object[] { "hello" });

}
